<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="userAdd">添加用户</button>
    </div>
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<table class="layui-hide" id="userTable" lay-filter="userTable"></table>
<script>
    layui.use('table', function(){
        var table = layui.table;
        var $ = layui.$;

        table.render({
            elem: '#userTable'
            ,url:'userListData'
            ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            ,cols: [[
                {type: 'checkbox', fixed: 'left'}
                ,{field:'userid', width:100, title: '用户编号', sort: true}
                ,{field:'username', width:200, title: '用户名'}
                ,{field:'phone', width:200, title: '手机号', sort: true}
                ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
            ]]
            ,page: true
        });

        //头工具栏事件
        table.on('toolbar(userTable)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'userAdd':
                    $.ajax({
                        url:'userEdit',
                        type:"get",
                        success:function(data) {
                            layer.open({
                                type: 1,
                                title: "添加用户",
                                content: data,
                                area: ['500px', '320px']
                            });
                        }
                    });
                    //var data = checkStatus.data;
                    //layer.alert(JSON.stringify(data));
                    break;
/*                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：'+ data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选': '未全选');
                    break;*/

                //自定义头工具栏右侧图标 - 提示
                case 'LAYTABLE_TIPS':
                    layer.alert('这是工具栏右侧自定义的一个图标按钮');
                    break;
            };
        });

        //监听行工具事件
        table.on('tool(userTable)', function(obj){
            var data = obj.data;
            //console.log(obj)
            if(obj.event === 'del'){
                layer.confirm('确定要删除该用户吗？', function(index){
                    $.ajax({
                        url: "userDelete",
                        type: "post",
                        data: "userid=" + data.userid,
                        dataType: "json",
                        success: function (data) {
                            if (data.code==0) {
                                table.reload("userTable");
                            } else {
                                layer.msg(data.msg);
                            }
                            layer.close(index);
                        }
                    });
                });
            } else if(obj.event === 'edit'){
                $.ajax({
                    url:'userEdit',
                    type:"get",
                    data:"userid=" + data.userid,
                    success:function(data) {
                        layer.open({
                            type: 1,
                            title: "编辑用户",
                            content: data,
                            area: ['500px', '320px']
                        });
                    }
                });
            }
        });
    });
</script>
